<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>客户</title>
    <link rel="stylesheet" href="\res\layui\css\layui.css" media="all">
    <style>
        body{margin: 10px;}
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
    </style>
</head>
<body>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">增加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete"><i id class="layui-icon"></i></button>
    </div>
</script>

<table class="layui-hide" id="demo" lay-filter="test"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="/res/layui/layui.js"></script>
<script>
    layui.config({
        version: '1615206508214' //为了更新 js 缓存，可忽略
    });
    var table;
    layui.use(['laydate', 'layer','table'], function(){
        var laydate = layui.laydate //日期
            ,layer = layui.layer //弹层
            ,$=layui.$

            table = layui.table //表格


            layer.msg('正在加载~~');

        //执行一个 table 实例
        table.render({
            elem: '#demo'
            ,height: 620
            , id: 'testReload'
            ,url: '/logisticsClient/findAll' //数据接口
            ,title: '客户表'
            ,page: true //开启分页
            ,toolbar: '#toolbarDemo'//开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,totalRow: true //开启合计行
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'clientId', title: 'ID', width:80,  fixed: 'left'}
                ,{field: 'clientName', title: '姓名', width: 90,  totalRow: true}
                ,{field: 'clientTel', title: '联系电话', width:140}
                ,{field: 'clientPwd', title: '密码', width:90}
                ,{field: 'clientAddress', title: '地址', width:160}
                ,{fixed: 'right', width: 165, align:'center', toolbar: '#barDemo'}
            ]]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2,
                        area: ['750px', '600px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/pages/PlaceAnOrder/addClient.html'
                    });
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        //向服务端发送删除指令
                        layer.confirm('确定删除勾选的嘛', function () {
                            var a = "";
                            $.each(data, function (index, item) {
                                a += item.clientId + ",";
                            }, "json")
                            let url = "/logisticsClient/deleteAllId";
                            let da = {id: a};
                            $.post(url, da, function (map) {
                                layer.msg(map.msg);
                                if (map.state == 1) {
                                    //  重新加载表格
                                    table.reload('testReload');
                                }
                            }, "json");
                        });
                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'detail') {
                layer.msg('查看操作');
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    //向服务端发送删除指令
                    let url="/logisticsClient/deleteById"
                    let data_ = {id:data.clientId}
                    console.info(data_);
                    $.post(url,data_,function (map){
                        if (map.state==1){
                            layer.msg("删除成功!!!")
                            obj.del(); //删除对应行（tr）的DOM结构
                            layer.close(index);
                        }
                    },"json");
                });
            } else if (layEvent === 'edit') {
                layer.open({
                    type: 2,
                    area: ['750px', '600px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/pages/PlaceAnOrder/addClient.html?id='+data.clientId,
                    success: function (layero, index) {

                        //当页面弹出完成时,怎么做
                        //alert(data.userName);
                    }
                });
            }
        });




    });
</script>
</body>
</html>
